/*
===========================================================================
This source file is part of DyLab (Dynamic Laboratory)
For the latest info, see http://dylab.googlecode.com

Copyright (c) 2006-2008 Lukas Krejci
(krejci.lukas@volny.cz)

This file is part of DyLab.

    DyLab is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DyLab is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DyLab.  If not, see <http://www.gnu.org/licenses/>.
===========================================================================
*/

#ifndef __dylab_VectorContainer_h__
#define __dylab_VectorContainer_h__

//===========================================================================================
#include <vector>

//===========================================================================================
#include <dylab/utils/containers/LinearContainerBase.h>

//===========================================================================================
namespace dylab {

    /** 
     */   
    template <
        class _ValueT
    >
    class VectorContainer 
        : public ContainerBase<std::vector<_ValueT> >
    { 
    public:
        typedef typename std::vector<_ValueT> VectorT;
        typedef typename LinearContainerBase<std::vector<_ValueT> > LinearContainerBaseT;
    public:        
        /// utility class (RIAA) for walking through all container elements
        class Iterator
            : public LinearContainerBaseT::Iterator
        {
        public:
            Iterator(ContainerBaseT & _c)
                : LinearContainerBaseT::Iterator(_c)
                    { }
        };
        //-----------------------------------------------------------------------------
    public:
        /// utility class (RIAA) for walking through all container elements
        class ConstIterator
            : public LinearContainerBaseT::ConstIterator
        {
        public:
            ConstIterator(const ContainerBaseT & _c)
                : LinearContainerBaseT::ConstIterator(_c)
                    { }
        };
        //-----------------------------------------------------------------------------
    public:
        /// scoped reader class
        class Reader 
            : public LinearContainerBaseT::Reader
        {
        public:
            Reader(const ContainerBaseT & _c)
                : LinearContainerBaseT::Reader(_c)
                    { }
            _ValueT operator [](uint32_t _index)
                    { 
                        DYLAB_ASSERT_MSGF2(_index < getCount(), "Index %1% out of range %2%.", _index, getCount());
                        return mSTLcontainer->at(_index); 
                    } 
            const _ValueT operator [](uint32_t _index) const
                    { 
                        DYLAB_ASSERT_MSGF2(_index < getCount(), "Index %1% out of range %2%.", _index, getCount());
                        return mSTLcontainer->at(_index); 
                    } 
        };
        //-----------------------------------------------------------------------------
    public:
        /// scoped writer class
        class Writer
            : public WriterBase
        {
        public:
            Writer(ContainerBaseT & _c)
                : WriterBase(_c)
                    { }
            void resize(uint32_t _minSize)
                    { mSTLcontainer->resize(_minSize); }
            void insertAtEnd(const _ValueT _value)
                    { mSTLcontainer->push_back(_value); }
            void removeEnd()
                    { mSTLcontainer->pop_back(); }
            void removeAll()
                    { mSTLcontainer->clear(); }
            _ValueT & operator [](uint32_t _index)
                    { 
                        DYLAB_ASSERT_MSGF2(_index < mSTLcontainer->size(), "Index %1% out of range %2%.", _index, (uint32_t)mSTLcontainer->size());
                        return mSTLcontainer->at(_index); 
                    } 
        };
        //-----------------------------------------------------------------------------

    public:

        _ValueT & operator[] (uint32_t _index)
                { return Writer(*this)[_index]; }                    

        const _ValueT operator[] (uint32_t _index) const
                { return Reader(*this)[_index]; }                    

        void resize(uint32_t _minSize) 
                { Writer(*this).resize(_minSize); }

        void insertAtEnd(const _ValueT _value)
                { Writer(*this).insertAtEnd(_value); }

        void removeEnd()
                { Writer(*this).removeEnd(); }

        void removeAll()
                { Writer(*this).removeAll(); }
    };    
}
//===========================================================================================
#endif // __dylab_VectorContainer_h__
